<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    Mirze<mirze@foxmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_mend_log.class.php 2011-07-06 14:23:24 $
 * @Show Note: 设备维修记录
 */
Class zy_mend_log extends TheAdmin
{
	/* DataBase object */
	public $db;

	/* View the data page limit items */
	public $pageSize = PAGENO;
	
	/**
	 *	Construct this module class.
	*/
	public function zy_mend_log()
	{
	}
	public function set()
	{
		$this->TheAdmin();
		//定义模版
		$this->tplFile ="zy_mend_log.tpl";
		$this->admin_user_id = $this->userRequest->get('admin_user_id');
		//取得数据库连接
		$this->db = Factory::getDB("zy_mend_log");
		//取得用户对于该类的权限
		$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
		$class_p_arr = $this->show_p();
		//取得该类的权值数组
		$class_method = $class_p_arr['class_method'];
		//取得用户操作菜单
		$menu = array();
		foreach ($class_method as $k=>$v)
		{
			if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
			{
				array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
			}
		}
		//用户有权菜单生成 可以添加其他操作
		$this->smarty->assign('menus',$menu);
		$this->smarty->assign('mend_state_arr',get_mend_state_arr());//状态
		$this->smarty->assign("main_menus",$this->get_user_menus());
	}
	
	/**
	 *	Call class construct method.
	*/
	public function __construct()
	{
			$this->zy_mend_log();
	}
	public function _display()
	{
		$this->smarty->display($this->tplFile);
	}
	/**
	 *	default method.
	*/
	public function show()
	{
		$col = "mend_log_id";
		$where = 1;
		$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
		//数据库执行sql语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if( 0 > $rowNo) goToIndex($rowNo);
		//分页
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		$arr = $this->db->getAll($where,$col,$numrows,$offset);
		admin_log($this->admin_user_id,__METHOD__,'查看',1);
		//数据库执行select语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if(0 > $arr ) goToIndex($arr);
		
		$this->smarty->assign('arr',$arr);
		//默认方法
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		$this->smarty->assign('act','list');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	add method.
	*/
	public function add()
	{
		$mc_mend_id = isset($_REQUEST['mc_mend_id']) ? intval($_REQUEST['mc_mend_id']) : 0;//维修ID
		//添加数据
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			if (empty($mc_mend_id)) {
				alertMsg('-2047');
			}
			$this->smarty->assign('mc_mend_id',$mc_mend_id);//维修ID
			//使用模版，展示提交界面
			$this->smarty->assign('act_title','添加');
			$this->smarty->assign('act','add');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			$temp = $this->getValue();
			$insrt = $this->db->insert_zy_mend_log($temp);
			$error_no = 0 > $insrt ?  $insrt :'-1501';
			admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($insrt < 0) {
				alertMsg($error_no, "index.php?c=zy_mend&m=browse&mc_mend_id={$mc_mend_id}");
			}else{
				redirectPage("index.php?c=zy_mend&m=browse&mc_mend_id={$mc_mend_id}");
			}
			//alertMsg($error_no,  'index.php?c=zy_mend');
		}
	}
	
	/**
	 *	edit method.
	*/
	public function edit()
	{
		//取得id，也就是唯一索引
		$mend_log_id = isset($_REQUEST['mend_log_id']) ? intval(trim($_REQUEST['mend_log_id'])) : 0 ;
		$mc_mend_id= isset($_REQUEST['mc_mend_id']) ? intval(trim($_REQUEST['mc_mend_id'])) : 0 ;
		//取得是否查看详情
		$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$result = $this->db->getOne($mend_log_id);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_mend_log');
			}
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			$this->smarty->assign('mc_mend_id',$mc_mend_id);//维修ID
			$this->smarty->assign('is_show',$is_show);
			$this->smarty->assign('act_title','修改');
			$this->smarty->assign('act','edit');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			//设置where条件
			$where = "mend_log_id=$mend_log_id";
			$temp = $this->getValue(1);
			$update = $this->db->update_zy_mend_log($temp,$where);
			admin_log($this->admin_user_id,__METHOD__,$where,2);
			$error_no = 0 > $update ?  $update :'-1502';
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($update < 0) {
				alertMsg($error_no, "index.php?c=zy_mend&m=browse&mc_mend_id={$mc_mend_id}");
			}else{
				redirectPage("index.php?c=zy_mend&m=browse&mc_mend_id={$mc_mend_id}");
			}
			//alertMsg($error_no,  'index.php?c=zy_mend');
		}
	}
	
	/**
	 *	browse method.
	*/
	public function browse() {
		//使用模版，展示提交界面
		$mend_log_id = isset($_GET['mend_log_id']) ? intval($_GET['mend_log_id']) : 0;
		$result = $this->db->getOne($mend_log_id);
		if(empty($result))
		{
			alertMsg('-2001');
		}
		foreach ($result AS $k=>$v)
		{
			$this->smarty->assign("{$k}",$v);
		}
		//$this->smarty->assign('is_show',$is_show);
		$this->smarty->assign('act_title','查看');
		$this->smarty->assign('act','browse');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	del method.
	*/
	public function del()
	{
		//取得id，也就是唯一索引
		$mend_log_id = isset($_REQUEST['mend_log_id']) ? intval(trim($_REQUEST['mend_log_id'])) : 0 ;
		$mc_mend_id = isset($_REQUEST['mc_mend_id']) ? intval(trim($_REQUEST['mc_mend_id'])) : 0 ;
		$where = "mend_log_id=$mend_log_id";
		$del=$this->db->delete_zy_mend_log($where);
		admin_log($this->admin_user_id,__METHOD__,$where,3);
		$error_no = 0 > $del ?  $del :'-1503';
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if ($del < 0) {
			alertMsg($error_no, "index.php?c=zy_mend&m=browse&mc_mend_id={$mc_mend_id}");
		}else{
			redirectPage("index.php?c=zy_mend&m=browse&mc_mend_id={$mc_mend_id}");
		}
		//alertMsg($error_no,  'index.php?c=zy_mend');
	}

	/**
	 *	import method.
	 */
	public function imports()
	{
		$opt = isset($_POST['opt']) ? intval($_POST['opt']) : 0 ;
		$obj_id = isset($_REQUEST['obj_id']) ? intval($_REQUEST['obj_id']) : 0 ; //mc_auth_id
		if($obj_id <= 0) {
			alertMsg2("操作有误", 'index.php?c=zy_mend');
		}

		if(empty($opt))	{
			$db_obj = Factory::getDB('zy_mend');
			$info = $db_obj->getOne($obj_id);
			$obj_lab_val = isset($info['mc_mend_no']) ? trim($info['mc_mend_no']) : '';

			$this->smarty->assign('act_title', "设备维修-维修明细导入");
			$this->smarty->assign('act', 'imports');
			$this->smarty->assign('control', 'zy_mend_log');
			$this->smarty->assign('obj_id', $obj_id);
			$this->smarty->assign('obj_lab', "维修单号");
			$this->smarty->assign('obj_lab_val', $obj_lab_val);
			$this->smarty->display($this->tplFile);
		} else {
			//1. 上传xls
			$savePath = UPLOAD_PATH . 'csv/zy_mend_log/';
			if(!file_exists($savePath)) {
				mk_dir($savePath);
			}
			$uploadRes = $this->upload_file_xls($savePath, 'fileName', array('csv'));
			if(0 == $uploadRes['state']) {
				alertMsg2($uploadRes['msg'], "index.php?c=zy_mend&m=browse&mc_mend_id={$obj_id}");
			}
			$fileName = $uploadRes['msg']; //上传成功后文件名

			//2. 上传成功后读取xls数据分析
			$item_pe = Factory::getItemClass('parse_excel');

			//获取xls文件数据
			$filePath = $savePath.$fileName;
			//$arr_xls = $item_pe->data_xls($filePath);
			//$out = $item_pe->data_parse_xls($arr_xls);

			$arr_csv = $item_pe->data_csv($filePath);
			$out = is_array($arr_csv['data']) ? $arr_csv['data'] : array();

			if(empty($out) || count($out) < 2) {
				alertMsg2("读取文件内容失败或数据为空", "index.php?c=zy_mend&m=browse&mc_mend_id={$obj_id}");
			}
			unset($out[0]); //去掉标题栏

			//3. 入库: 24项，带表头，去第一列序号
			foreach($out as $k => $v) {
				if(!empty($v)) {
					$val['mc_mend_id'] = $obj_id; //
					$val['admin_user_id'] = $this->admin_user_id; //录入用户：后台登录用户
					if(!empty($v[0]) && !empty($v[1])) {
						$val['mc_no'] = $v[0];
						$val['mend_comp'] = $v[1];
						$val['mc_name'] = $v[2];
						$val['mc_model'] = $v[3];
						$val['mc_number'] = intval($v[4]); //数量
						$val['mc_mend_cost'] = $v[5];
						$val['mend_state'] = $v[6]; //维修状态: 0 维修中 1 维修完成
						$val['mend_finsh'] = isset($v[7]) ? strtotime($v[7]) : ''; //完成日期
						$val['mend_mc_info'] = trim($v[8]);
						$val['mend_info'] = trim($v[9]);

						$resId = $this->db->insert_zy_mend_log($val);
					}
					unset($val);
				}
			}
			//redirectPage('index.php?c=zy_mend');
			alertMsg2('导入完成', "index.php?c=zy_mend&m=browse&mc_mend_id={$obj_id}");
		}
	}
	
	/**
	 *	get request method.
	*/
	public function &getValue($isEdit=0)
	{
		$temp = array();
		$check = Factory::getBaseClass("checkBase");
		if($isEdit){
			$temp['mend_log_id'] = isset($_POST['mend_log_id']) ? intval($_POST['mend_log_id']) : 0;
			if (empty($temp['mend_log_id'])) {
				alertMsg('-2048');
			}
		} else {
			$temp['mend_log_id'] = "";
			$temp['admin_user_id'] = $this->admin_user_id;
			$temp['mc_mend_id'] = isset($_POST['mc_mend_id']) ? intval($_POST['mc_mend_id']) : 0;//维修ID
			if (empty($temp['mc_mend_id'])) {
				alertMsg('-2047');
			}
		}
		//补充其他的字段
		$temp['mc_no'] = isset($_POST['mc_no']) ? htmlspecialchars(trim($_POST['mc_no'])): '';//设备自编号
		if (empty($temp['mc_no'])) {
			alertMsg('-2049');
		}
		$temp['mc_name'] = isset($_POST['mc_name']) ? htmlspecialchars(trim($_POST['mc_name'])) : '';//设备名称
		$temp['mc_model'] = isset($_POST['mc_model']) ? htmlspecialchars(trim($_POST['mc_model'])) : '';//规格型号
		$temp['mc_number'] = isset($_POST['mc_number']) ? intval($_POST['mc_number']) : 1;//数量
		$temp['mend_mc_info'] = isset($_POST['mend_mc_info']) ? htmlspecialchars(trim($_POST['mend_mc_info'])) : '';//修造项目内容
		$temp['mc_mend_cost'] = isset($_POST['mc_mend_cost']) ? sprintf("%01.2f",$_POST['mc_mend_cost']) : 0;//预计修改费用
		if (empty($temp['mc_mend_cost'])) {
			alertMsg('-2050');
		}
		$temp['mend_comp'] = isset($_POST['mend_comp']) ? htmlspecialchars(trim($_POST['mend_comp'])) : '';//修理单位
		$temp['mend_state'] = isset($_POST['mend_state']) ? intval($_POST['mend_state']) : 0;//维修状态 0维修中 1维修完成
		$temp['mend_finsh'] = isset($_POST['mend_finsh']) ? strtotime($_POST['mend_finsh']) : 0;//维修完成日期
		$temp['mend_info'] = isset($_POST['mend_info']) ? htmlspecialchars(trim($_POST['mend_info'])) : '';//备注
		admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
		return $temp;
	}
	/**
	 *	get class method and popedom.
	*/
	public function show_p()
	{
		//该方法为，提供用户使用方法定义，非web用户权限相关不用写
		return array(
			/* 定义该类中文名称*/
			'class_name'=>array(__CLASS__,'设备维修记录'),
			'class_method' =>array(
				//array（方法名，中文解释，权值,是否为菜单）
				'show'  => array('show','列表',1,true),
				'add'  => array('add','添加',2,true),
				'edit'  => array('edit','编辑',4,false),
				'del'  => array('del','删除',8,false),
				'browse' => array('browse', '查看', 16, false),
				'imports'  => array('imports','导入',32,false),
				'show_p'=> array('show_p','',0,false)
			)
		);
	}
}

?>